<%@ tag description="put the tag description here" pageEncoding="UTF-8" import="org.apache.log4j.Logger, java.util.Collection, java.util.Iterator, javax.servlet.http.HttpServletRequest"%>

<%@ attribute name="collection" required="true"%>
<%@ attribute name="pageSize" required="true" %>
<%@ attribute name="action" required="true" %>
<%@ attribute name="additionalParams" required="false" %>
<%@ attribute name="parameterName" required="false" description="Current page number is transferred under this request parameter name. [default = page]"%>

<%!
/*
private static final FIRST = "&lt;&lt;";
private static final PREVIOUS = "&lt;";
private static final NEXT = "&gt;";
private static final LAST = "&gt;&gt;";
*/
private Logger log = Logger.getLogger(this.getClass());

private Collection findCollection(HttpServletRequest request) throws ServletException{
    Collection aCollection = (Collection) request.getSession().getAttribute(collection);
    if (aCollection==null) aCollection = (Collection) request.getAttribute(collection);
    if (aCollection==null) {
        log.error("Couldn't find a " + collection + " in any scope.");
        throw new ServletException("Collection " + collection + " is not available.");
    }
    return aCollection;
}

private int getCurrentPage(HttpServletRequest request) {
    if (parameterName==null) parameterName="page";
    String page = request.getParameter(parameterName);
    if (page == null) {
        return 0;
    } else {
        return Integer.parseInt(page);
    }
}

private int getPageS(){
    return Integer.parseInt(pageSize);
}

private Collection getSubCollection(Collection aCollection, int aPage, int aPageSize){
    Collection subCollection = null;
    
    try{
    subCollection = aCollection.getClass().newInstance();
    Object[] tempArr = aCollection.toArray();
    for(int i=aPage*aPageSize; i<(aPage+1)*aPageSize && i<aCollection.size(); i++){
        subCollection.add(tempArr[i]);
    }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    
    return subCollection;
}


private String getLinkToPage(HttpServletRequest request, int page, int pagesCount){
    StringBuffer o= new StringBuffer(300);
    o.append(request.getContextPath());
    o.append(action);
    if (!action.endsWith(".do")) o.append(".do");
    o.append("?");
    if (additionalParams!=null) {
        o.append(additionalParams);
        o.append("&");
    }
    o.append(parameterName);
    o.append("=");
    if (page<0) page=0;
    if (page>=pagesCount) page = pagesCount-1;
    o.append(page);
    return o.toString();
}

private int getPagesCount(Collection a, int pageSize)
{
      double elemCount = (double) a.size();
      return (int) Math.ceil( elemCount/pageSize );
}
%>

<%

Collection a = findCollection(request);
int curPage = getCurrentPage(request);
int pageSize = getPageS();
int pagesCount = getPagesCount(a, pageSize);
Collection b = getSubCollection(a, curPage, pageSize);
request.setAttribute(collection,b);

%>

<a href="<%=getLinkToPage(request, 0, pagesCount)%>">&lt;&lt;</a> 
<a href="<%=getLinkToPage(request, curPage-1, pagesCount)%>">&lt;</a>

<%
for (int i=0; i<pagesCount; i++){
    if (i==curPage) out.print("<b>");
    
%>
<a href="<%=getLinkToPage(request, i, pagesCount)%>"><%=i+1%></a>
<%
    if (i==curPage) out.print("</b>");
}
%>


<a href="<%=getLinkToPage(request, curPage+1, pagesCount)%>">&gt;</a>
<a href="<%=getLinkToPage(request, pagesCount, pagesCount)%>">&gt;&gt;</a>
